// 老师查询班级列表
import {serviceModulePrefix, useDecorateApi} from "@/utils/tools";
import request from "@/utils/request";
import dayjs from "dayjs";
import axios from "axios";


// 签到列表管理
export const apiClockInList = (formData, opts) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign/list`,
  method: 'post',
  data: {...unref(formData)}
}), {
  toast: false,
  ...opts
})


// 新增，编辑签到
export const apiClockIn = (formData, opts) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign`,
  method: 'post',
  data: {
    ...unref(formData),
    startTime: +unref(formData).startTime ? dayjs(+unref(formData).startTime).format('YYYY-MM-DD HH:mm:ss') : unref(formData).startTime,
    endTime: +unref(formData).endTime ? dayjs(+unref(formData).endTime).format('YYYY-MM-DD HH:mm:ss') : unref(formData).endTime,
  }
}), {
  ...opts
})


// 强制中断
export const apiClockAbort = (id, opts) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign/force/end/${id}`,
  method: 'get',
}), {...opts})


// 删除签到
export const apiClockDel = (id, opts) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign/delete/${id}`,
  method: 'delete',
}), {...opts})

// 签到详情
export const apiClockDetail = (id, opts) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign/detail/${id}`,
  method: 'get',
}), {
  dataCleaning: res => res.data,
  ...opts
})

// 导出签到记录
export function bkClockRecordExport(id) {
  return request({
    url: `${serviceModulePrefix}/sign/export/${id}`,
    responseType: 'blob'
  })
}

// 获取已签到列表
export const apiClockSignList = (id, opts) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign/sign/list`,
  method: 'post',
  data: {
    id
  }
}), {
  ...opts
})


// 获取未签到列表
export const apiClockUnSignList = (id, opts) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign/list`,
  method: 'post',
  data: {
    id
  }
}), {
  ...opts
})


// 获取学生应签到的签到列表
export const apiStudentSignHisList = (formData, opts) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign/student/list`,
  params: {
    ...unref(formData)
  }
}), {
  ...opts
})

// 提交补签
export const apiStudentFixSign = (formData, opts) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign/makeup/apply`,
  method: 'post',
  data: {
    ...unref(formData)
  }
}), {
  ...opts
})


// 通过、驳回补签
export const apiTeacherPutSign = (formData, opts) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign/makeup/check/${formData.id}/${formData.type}`,
}), {
  ...opts
})


// 查询考勤记录
export const apiQuerySignHis = (formData, opts) => useDecorateApi(request({
  url: `${serviceModulePrefix}/unifiedClass/attend`,
  method: 'post',
  data: {
    ...unref(formData)
  }
}), {
  dataCleaning: res => res.data.map((item, index) => ({
    ...item,
    attendTime: dayjs(item.attendTime).format('YYYY-MM-DD HH:mm:ss'),
    index: index + 1
  })),
  ...opts
})


// 刷新签到码
export const apiUpdateSignCode = (signId) => {
  const cancel = axios.CancelToken.source()

  const {promise, dataRef} = useDecorateApi(request({
    url: `${serviceModulePrefix}/sign/apply/code/${signId}?timestamp=${Date.now()}`,
    cancelToken: cancel.token,
    timeout: 3400
  }), {
    toast: false,
    isSuccess: res => !!res,
    dataCleaning: res => {
      if (typeof res === 'string') {
        const [img = '', index = '', stamp = ''] = res.split(':')
        return {
          code: 200,
          data: {
            img: `data:image/png;base64,${img}`,
            index,
            stamp
          }
        }
      }
      if (typeof res === 'object') return res
      return {
        code: 500
      }
    }
  })

  return {
    promise,
    dataRef,
    cancel
  }
}


// 刷新签到码
export const apiQuerySignCodeStatus = (signId) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign/code/remain/${signId}?timestamp=${Date.now()}`,
}), {toast: false})


// 拉去最新签到详情信息
export const apiFetchCurrSignInfo = (signId, opts = {}) => useDecorateApi(request({
  url: `${serviceModulePrefix}/sign/polymer/info/${signId}`,
}), {toast: false, ...opts})
